package com.mcptt.notify.presence;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.product.func.call.CallTimer;
import com.mcptt.comm.CommonFunc;
import com.mcptt.db.model.GroupInfo;
import com.mcptt.db.model.SessionInfo;
import com.mcptt.db.model.UserInfo;

public class Affiliation
{
	//    public static final String Name_Status = "status";

	public static final String Name = "affiliation";
	private static final String Name_Group = "group";
	private static final String Name_Status = "status";
	private static final String Name_Expires = "expires";
	private static final String Valid_Status = "affiliated";

	private String group = null;


	private String status = null;
	private String expires = null;

	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		this.group = elem.getAttributeValue(Name_Group);
		this.status = elem.getAttributeValue(Name_Status);
		this.expires = elem.getAttributeValue(Name_Expires);
	}

	public SessionInfo toSessionInfo(GroupInfo groupInfo,String mcpttId)
	{
		SessionInfo info = new SessionInfo(groupInfo,mcpttId,SessionInfo.Msgtype_Affiliation,"Affiliation",0);
		return info;
	}
	public GroupInfo toGroupInfo()
	{
		GroupInfo groupInfo = new GroupInfo(GroupInfo.GroupType_Incomplete,group,group,GroupInfo.Group_Id_Root,0, CallTimer.T180_DEFAULT,0);
		return groupInfo;
	}
	public boolean isValid()
	{
		if(Valid_Status.equals(status)
				&& CommonFunc.isExpire(expires))
		{
			return true;
		}
		return false;
	}
	public String getGroup()
	{
		return group;
	}

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("<mcpttPI10:").append(Name).append(" ");
		strBuf.append(Name_Group).append("=\"").append(group).append("\" ");
		strBuf.append(Name_Status).append("=\"").append(status).append("\" ");
		strBuf.append(Name_Expires).append("=\"").append(expires).append("\" ");
		strBuf.append("/>");

		return strBuf.toString();
	}
}
